package fr.labri.macchiato.core.technical;

import java.util.ArrayList;
import java.util.List;

/**
 * Observable class
 * 
 * @author Paul SARRAUTE
 *
 */
public class MObservable {
	
	/**
	 * The list of objects observing this one
	 */
	protected transient List<MObserver> observers;
	
	/**
	 * Default constructor. Initializes the list of observers
	 */
	public MObservable() {
		observers = new ArrayList<MObserver>();
	}
	
	/**
	 * Adds an observer
	 * @param o
	 */
	public void addObserver(MObserver o) {
		observers.add(o);
	}
	
	/**
	 * Removes an observer
	 * @param o
	 */
	public void removeObserver(MObserver o) {
		observers.remove(o);
	}
	
	/**
	 * Notifies all observers 
	 */
	public void notifyObservers() {
		notifyObservers(null);
	}
	
	/**
	 * Notifies all observers, passing an object as argument
	 * @param arg
	 */
	public void notifyObservers(Object arg) {
		for (MObserver o : observers)
			o.update(this, arg);
	}
}
